package indi.lddsdu.spider.conponent;

import indi.lddsdu.spider.enums.ContentType;
import indi.lddsdu.spider.exceptions.CreateFileException;
import indi.lddsdu.spider.exceptions.FileWriteException;
import indi.lddsdu.spider.utils.ProcessUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Created by jack on 18/6/4.
 */
@Component
public class ViewPipeline implements Pipeline {
    Lock picReadLock = new ReentrantLock();
    Lock htmlReadLock = new ReentrantLock();
    private Logger logger = LoggerFactory.getLogger(getClass());
    private AtomicInteger picIndex = new AtomicInteger(0);
    private AtomicInteger htmlIndex = new AtomicInteger(0);

    @Autowired
    private ProcessUtil processUtil;

    //需要通过 resultItems中的数据传递
    public void process(ResultItems resultItems, Task task) {
        ContentType type = resultItems.get("type");
        String url = resultItems.get("url");
        if(type == ContentType.IMAGE){
            byte[] content = resultItems.get("content");
            try {
                picReadLock.lock();
                processUtil.dealWithPic(url, content, picIndex);
                picIndex.getAndIncrement();
                picReadLock.unlock();
            } catch (CreateFileException e) {
                logger.error("error create file " + e.getMessage());
            } catch (FileWriteException e) {
                logger.error("error write file " + e.getMessage());
            }
        }

        if(type == ContentType.TEXT){
            String title = resultItems.get("title");
            String text = resultItems.get("text");
            try {
                htmlReadLock.lock();
                processUtil.dealWithText(title, url, text, htmlIndex);
                htmlIndex.getAndIncrement();
                htmlReadLock.unlock();
            } catch (CreateFileException e) {
                logger.error("error create file " + e.getMessage());
            } catch (FileWriteException e) {
                logger.error("error write file " + e.getMessage() );
            }
        }
    }
}
